package com.ruoyi.project.er.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.er.domain.*;
import com.ruoyi.project.er.mapper.PaeaErErrorPointRelMapper;
import com.ruoyi.project.er.service.IPaeaErErrorInstRelService;
import com.ruoyi.project.er.service.IPaeaErErrorPointRelService;
import com.ruoyi.project.er.mapper.PaeaErErrorMapper;
import com.ruoyi.project.er.service.IPaeaErPointService;
import com.ruoyi.project.er.service.IPaeaErErrorService;
import com.ruoyi.project.paea.paeaDsTrade.domain.PaeaDsTrade;
import com.ruoyi.project.paea.paeaDsTrade.service.IPaeaDsTradeService;
import com.ruoyi.project.sdi.sdiImage.domain.SdiImage;
import com.ruoyi.project.sdi.sdiImage.service.ISdiImageService;
import com.ruoyi.project.wf.service.IWfProcessInstService;
import com.ruoyi.project.wf.service.IWfWorkItemService;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 督办查询Service业务层处理
 *
 * @author XQ.XU
 * @date 2020-03-22
 */
@Service
public class PaeaErErrorServiceImpl extends ServiceImpl<PaeaErErrorMapper, PaeaErError> implements IPaeaErErrorService
{
    @Resource
    private PaeaErErrorMapper paeaErErrorMapper;
    @Resource
    private PaeaErErrorPointRelMapper paeaErErrorPointRelMapper;
    @Resource
    private IPaeaErErrorPointRelService paeaErErrorPointRelService;
    @Resource
    private IPaeaErPointService paeaErPointService;
    @Resource
    private IPaeaErErrorInstRelService paeaErErrorInstRelService;
    @Resource
    private IWfProcessInstService wfProcessInstService;
    @Resource
    private IWfWorkItemService wfWorkItemService;
    @Resource
    private IPaeaDsTradeService paeaDsTradeService;
    @Resource
    private ISdiImageService sdiImageService;
    /**
     * 查询督办查询
     * @param id 督办查询ID
     * @return 督办查询
     */
    @Override
    public PaeaErError selectPaeaErErrorById(String id)
    {
        return paeaErErrorMapper.selectPaeaErErrorById(id);
    }

    /**
     * 查询督办查询列表
     *
     * @param paeaErError 督办查询
     * @return 督办查询
     */
    @Override
    public List<PaeaErError> selectPaeaErErrorList(PaeaErError paeaErError)
    {
        return paeaErErrorMapper.selectPaeaErErrorList(paeaErError);
    }

    /**
     * 新增督办查询
     *
     * @param paeaErError 督办查询
     * @return 结果
     */
    @Override
    public int insertPaeaErError(PaeaErError paeaErError)
    {
        paeaErError.setCreateTime(DateUtils.getNowDate());
        return paeaErErrorMapper.insertPaeaErError(paeaErError);
    }

    /**
     * 修改督办查询
     *
     * @param paeaErError 督办查询
     * @return 结果
     */
    @Override
    public int updatePaeaErError(PaeaErError paeaErError)
    {
        paeaErError.setUpdateTime(DateUtils.getNowDate());
        return paeaErErrorMapper.updatePaeaErError(paeaErError);
    }

    /**
     * 删除督办查询对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaeaErErrorByIds(String ids)
    {
        return paeaErErrorMapper.deletePaeaErErrorByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除督办查询信息
     *
     * @param id 督办查询ID
     * @return 结果
     */
    @Override
    public int deletePaeaErErrorById(String id)
    {
        return paeaErErrorMapper.deletePaeaErErrorById(id);
    }

    @Override
    public int deletePaeaErError(@NotNull List<String> errIds) {
        for(String errId:errIds){
            PaeaErError error = paeaErErrorMapper.selectPaeaErErrorById(errId);
            if(error==null){
                return 0;
            }
            paeaErErrorPointRelService.deletePaeaErErrorPointRelByErrId(errId);
            List<PaeaErErrorInstRel> errInstList=paeaErErrorInstRelService.selectPaeaErErrorInstRelsByErrId(errId);
            if(errInstList==null){
                errInstList=new ArrayList<PaeaErErrorInstRel>();
            }
            for(PaeaErErrorInstRel rel:errInstList){
                wfWorkItemService.deleteWfWorkItemByProcInstId(rel.getProcInstId());
                wfProcessInstService.deleteWfProcessInstById(rel.getProcInstId());
            }
            paeaErErrorInstRelService.deletePaeaErProcInstRelByErrId(errId);
            paeaErErrorMapper.deletePaeaErErrorById(errId);
        }
        return 1;
    }

    @Override
    public List<PaeaErError> selectPaeaErErrorHisListByErrId(String id) {
        PaeaErError error =this.selectPaeaErErrorById(id);
        return this.list(new QueryWrapper<PaeaErError>()
                .eq("tr_date",error.getTrDate())
                .eq("org_id",error.getOrgId())
                .eq("teller_id",error.getTellerId())
                .eq("vch_no",error.getVchNo())
                .ne("id",error.getId())
                .orderByAsc("create_time"));
        //return this.paeaErErrorMapper.selectPaeaErErrorHisListByErrId(error);
    }
    @Override
    public PaeaDsTrade selectPaeaErErrorByErrId(String errId) {
        PaeaErError erError= this.selectPaeaErErrorById(errId);
        if(StringUtils.isNotEmpty(erError.getTradeId())){
            return paeaDsTradeService.selectPaeaDsTradeById(erError.getTradeId());
        }
        if(StringUtils.isNotEmpty(erError.getVchNo())){
            List<PaeaDsTrade> list=paeaDsTradeService.list(new QueryWrapper<PaeaDsTrade>().eq("trDate",erError.getTrDate()).eq("vchNo",erError.getTrDate()));
            return (list!=null && list.size()>0 )?list.get(0):null;
        }
        return null;
    }

    @Override
    public List<SdiImage> selectSdiImageListByErrId(String errId) {
       PaeaErError error = this.getById(errId);
       if(StringUtils.isNotEmpty(error.getTradeId())){
          PaeaDsTrade trade =paeaDsTradeService.selectPaeaDsTradeById(error.getTradeId());
          if(StringUtils.isEmpty(trade.getImageId())){
              trade =paeaDsTradeService.selectPaeaDsTrade4Image(trade);
          }
          if(null!=trade){
              return sdiImageService.getSdiImagesByPaeaDsTrade(trade);
          }
       }
       return new ArrayList<SdiImage>();

    }


}
